/**
 * Created by henian.xu on 2018/9/5.
 * 这是饿了么的代码
 */

import Emitter from 'element-ui/src/mixins/emitter';
import ElSelectMenu from 'element-ui/packages/select/src/select-dropdown';
import Clickoutside from 'element-ui/src/utils/clickoutside';
import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event';
export default {
    mixins: [Emitter],
    components: { ElSelectMenu },
    directives: { Clickoutside },
    data() {
        return {
            visible: false,
            inputWidth: 0,
        };
    },
    watch: {
        visible(val) {
            if (!val) {
                this.broadcast('ElSelectDropdown', 'destroyPopper');
            } else {
                this.broadcast('ElSelectDropdown', 'updatePopper');
            }
        },
    },
    methods: {
        doDestroy() {
            this.$refs.popper && this.$refs.popper.doDestroy();
        },
        handleFocus() {
            this.visible = true;
        },
        handleClose() {
            this.visible = false;
        },
        resetInputWidth() {
            this.inputWidth = this.$refs.reference.$el.getBoundingClientRect().width;
        },

        handleResize() {
            this.resetInputWidth();
            // if (this.multiple) this.resetInputHeight();
            this.broadcast('ElSelectDropdown', 'updatePopper');
        },
    },
    mounted() {
        addResizeListener(this.$el, this.handleResize);
    },
    beforeDestroy() {
        if (this.$el && this.handleResize) removeResizeListener(this.$el, this.handleResize);
    },
};
